<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import PlotCanvas from '@/components/PlotCanvas.vue'
import ExpressionPanel from '@/components/ExpressionPanel.vue'
import ContourTable from '@/components/ContourTable.vue'
import RenderSettings from '@/components/RenderSettings.vue'
import { useExpressionStore } from '@/stores/expressionStore'

const expressionStore = useExpressionStore()
const { expressions } = storeToRefs(expressionStore)

onMounted(() => {
  expressionStore.initialiseDefaults()
})

const readyCount = computed(() =>
  expressions.value.filter((entry) => entry.status === 'ready' && entry.visible).length,
)

const errorCount = computed(() => expressions.value.filter((entry) => entry.status === 'error').length)
</script>

<template>
  <div class="app-shell">
    <header class="app-header">
      <div>
        <h1 class="app-title">Implicit Plotter · 隐式函数绘图</h1>
        <p class="app-subtitle">阶段三 · 前端交互 / 实时渲染</p>
      </div>
      <div class="app-metrics">
        <span class="metric">
          <strong>{{ expressions.length }}</strong>
          <span>表达式</span>
        </span>
        <span class="metric metric--ready">
          <strong>{{ readyCount }}</strong>
          <span>已渲染</span>
        </span>
        <span class="metric metric--error">
          <strong>{{ errorCount }}</strong>
          <span>异常</span>
        </span>
      </div>
    </header>

    <main class="app-main">
      <section class="plot-region">
        <PlotCanvas />
      </section>
      <aside class="side-panel">
        <ExpressionPanel />
        <RenderSettings />
        <ContourTable />
      </aside>
    </main>
  </div>
</template>

<style scoped>
.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: #0b1120;
  color: #e2e8f0;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 1.5rem 2rem;
  background: rgba(15, 23, 42, 0.92);
  border-bottom: 1px solid rgba(148, 163, 184, 0.12);
  gap: 1.5rem;
}

.app-title {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 700;
}

.app-subtitle {
  margin: 0.35rem 0 0;
  font-size: 0.95rem;
  color: #94a3b8;
}

.app-metrics {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.metric {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.6rem 0.9rem;
  border-radius: 0.75rem;
  background: rgba(148, 163, 184, 0.12);
  min-width: 4.5rem;
}

.metric strong {
  font-size: 1.4rem;
  line-height: 1.2;
}

.metric span:last-child {
  font-size: 0.75rem;
  color: #94a3b8;
}

.metric--ready {
  background: rgba(34, 197, 94, 0.12);
  color: #4ade80;
}

.metric--error {
  background: rgba(248, 113, 113, 0.12);
  color: #f87171;
}

.app-main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 2.5fr) minmax(320px, 1fr);
  gap: 1.75rem;
  padding: 1.75rem 2rem 2.5rem;
}

.plot-region,
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  min-width: 0;
}

.side-panel {
  overflow: visible;
}

@media (max-width: 1200px) {
  .app-main {
    grid-template-columns: 1fr;
  }

  .side-panel {
    order: -1;
  }
}

@media (max-width: 720px) {
  .app-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .app-metrics {
    width: 100%;
    justify-content: space-between;
  }
}
</style>
